package com.sgb.kjwl.view.widget.pick;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.LayoutPickDetailRuleBinding;
import com.sgb.kjwl.model.entity.pick.TenderDictEntity3;
import com.sgb.kjwl.model.entity.pick.TenderInfoBasicEntity3;
import com.sgb.kjwl.model.entity.pick.TenderInfoMainEntity3;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.sgb.kjwl.view.ui.adapter.pick.PickDetailDictAdapter3;
import com.sgb.kjwl.view.ui.adapter.pick.PickDetailFileAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: 张驰
 * @CreateDate: 006
 * @UpdateUser: 张驰
 * @UpdateDate: 2020年09月24
 * @UpdateRemark: 报名详情页中投标人规则模块布局
 * @Version: 招采 2.7.2
 */
public class PickDetailRuleView extends RelativeLayout {
    private Context mContext;
    private LayoutPickDetailRuleBinding binding;
    private PickDetailDictAdapter3 pickDetailDictAdapter3;
    private PickDetailFileAdapter pickDetailFileAdapter;
    private String title;

    public PickDetailRuleView(Context context, String title) {
        super(context);
        this.mContext = context;
        initView(title);
    }

    public PickDetailRuleView(Context context, @Nullable AttributeSet attrs, String title) {
        super(context, attrs);
        this.mContext = context;
        initView(title);
    }

    public PickDetailRuleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, String title) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(title);
    }

    private void initView(String title) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.layout_pick_detail_rule, this, true);
        binding.title.setText(title);
    }

    public void showInfoView(List<TenderInfoBasicEntity3> tenderInfoBasicEntity3List,String title) {
        if (tenderInfoBasicEntity3List == null) {
            binding.pickRecycler.setVisibility(View.GONE);
            return;
        }
        if (tenderInfoBasicEntity3List.size() == 0) {
            binding.pickRecycler.setVisibility(View.GONE);
            return;
        }
        this.title = title;
        if (pickDetailDictAdapter3 == null) {
            pickDetailDictAdapter3 = new PickDetailDictAdapter3(mContext, tenderInfoBasicEntity3List,title);
        }
        List<TenderInfoBasicEntity3> tempList = new ArrayList<>();
        if (tenderInfoBasicEntity3List.size() > 0) {
            for (TenderInfoBasicEntity3 entity3 : tenderInfoBasicEntity3List) {
                List<TenderDictEntity3> datas = entity3.getDatas();
                List<TenderDictEntity3> tempChildList = new ArrayList<>();
                if (datas != null && datas.size() > 0) {
                    for (TenderDictEntity3 data : datas) {
                        if (!TextUtils.isEmpty(data.getName()) || !TextUtils.isEmpty(data.getValue())) {
                            if (TextUtils.isEmpty(data.getName())) {
                                data.setName("");
                            }
                            if (TextUtils.isEmpty(data.getValue())) {
                                data.setValue("");
                            }
                            tempChildList.add(data);
                        }
                    }
                }
                entity3.setDatas(tempChildList);
                tempList.add(entity3);

                if ("八、招标规则".equals(entity3.getGroupName()) || "十、清单组价".equals(entity3.getGroupName())) {
                    if (entity3.getChildren() != null && entity3.getChildren().size() > 0) {
                        for (TenderInfoBasicEntity3 basicEntity3 : (List<TenderInfoBasicEntity3>) entity3.getChildren()) {
                            List<TenderDictEntity3> basicEntity3Datas = basicEntity3.getDatas();
                            List<TenderDictEntity3> tempChildList1 = new ArrayList<>();
                            if (basicEntity3Datas != null && basicEntity3Datas.size() > 0) {
                                for (TenderDictEntity3 data : basicEntity3Datas) {
                                    if (!TextUtils.isEmpty(data.getValue())) {
                                        tempChildList1.add(data);
                                    }
                                }
                            }
                            basicEntity3.setDatas(tempChildList1);
                            tempList.add(basicEntity3);
                        }
                    }
                }

            }
        }

        binding.pickRecycler.setSectionedRecyclerViewAdapter(pickDetailDictAdapter3);
        binding.pickRecycler.setLoadingMoreEnabled(false);
        binding.pickRecycler.setPullRefreshEnabled(false);
        binding.pickRecycler.setLayoutManager(new GridLayoutManager(mContext, 1));
        binding.pickRecycler.setAdapter(pickDetailDictAdapter3);
        pickDetailDictAdapter3.setData(tempList);

    }

    public void showMainInfoView(TenderInfoMainEntity3 tenderInfoMainEntity3, TenderDictEntity3 tenderRar, List<TenderDictEntity3> tenderDictEntity3List, TenderDetailsActivity3 activity) {
        binding.pickTitle.setText(tenderInfoMainEntity3.getName());
        if (tenderInfoMainEntity3.getBiddingOrganizationForm() == 1) {
            binding.pickComName.setText("发布单位：" + tenderInfoMainEntity3.getComName());
        } else if (tenderInfoMainEntity3.getBiddingOrganizationForm() == 2) {
            binding.pickComName.setText("发布单位：" + tenderInfoMainEntity3.getAgencyName());
        }
        binding.pickTime.setText("发布时间：" + tenderInfoMainEntity3.getCreated());
        if(TextUtils.isEmpty(tenderInfoMainEntity3.getRemark())){
            binding.pickRemark.setVisibility(View.GONE);
        }else{
            binding.pickRemark.setText("\u3000" + "\u3000" + tenderInfoMainEntity3.getRemark());
        }
        binding.fileDown.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tenderRar != null && !TextUtils.isEmpty(tenderRar.getFileUrl())) {
                    //调用系统浏览器下载
                    Uri uri = Uri.parse(tenderRar.getFileUrl());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    mContext.startActivity(intent);
                } else {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.show(mContext, "文件正在生成中，请稍候再尝试下载");
                        }
                    });
                }
            }
        });

        if (tenderDictEntity3List == null) {
            binding.pickTitleFile.setVisibility(View.GONE);
            binding.pickRecyclerFile.setVisibility(View.GONE);
            return;
        }
        if (tenderDictEntity3List.size() == 0) {
            binding.pickTitleFile.setVisibility(View.GONE);
            binding.pickRecyclerFile.setVisibility(View.GONE);
            return;
        }

        if(pickDetailFileAdapter == null){
            pickDetailFileAdapter = new PickDetailFileAdapter(mContext,tenderDictEntity3List,title);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        binding.pickRecyclerFile.setLayoutManager(layoutManager);
        binding.pickRecyclerFile.setAdapter(pickDetailFileAdapter);
    }

}
